package com.macro.mall.tiny.modules.bms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.modules.bms.mapper.AdvertPositionMapper;
import com.macro.mall.tiny.modules.bms.model.AdvertPosition;
import com.macro.mall.tiny.modules.bms.service.AdvertPositionService;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 广告位置表 服务实现类
 * </p>
 *
 * @author macro
 * @since 2025-11-21
 */
@Service
public class AdvertPositionServiceImpl extends ServiceImpl<AdvertPositionMapper, AdvertPosition> implements AdvertPositionService {

    @Autowired
    private AdvertPositionMapper advertPositionMapper;

    @Override
    public CommonPage<AdvertPosition> getPageList(String name, LocalDateTime startTime, LocalDateTime endTime, Integer pageNum, Integer pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<AdvertPosition> advertPositionList = advertPositionMapper.selectPageList(name, startTime, endTime, offset, pageSize);
        int totalCount = advertPositionMapper.selectTotalCount(name, startTime, endTime);
        int totalPage = (int) Math.ceil((double) totalCount / pageSize);

        Page<AdvertPosition> page = new Page<>(pageNum, pageSize);
        page.setRecords(advertPositionList);
        page.setTotal(totalCount);
        page.setPages(totalPage);

        return CommonPage.restPage(page);
    }

    @Override
    public boolean create(AdvertPosition advertPosition) {
        advertPosition.setCreatedAt(LocalDateTime.now());
        advertPosition.setUpdatedAt(LocalDateTime.now());
        return this.save(advertPosition);
    }

    @Override
    public boolean update(Long id, AdvertPosition advertPosition) {
        advertPosition.setId(id.intValue());
        advertPosition.setUpdatedAt(LocalDateTime.now());
        return this.updateById(advertPosition);
    }

    @Override
    public boolean delete(Long id) {
        AdvertPosition advertPosition = new AdvertPosition();
        advertPosition.setId(id.intValue());
        advertPosition.setDelFlag(1);
        advertPosition.setUpdatedAt(LocalDateTime.now());
        return this.updateById(advertPosition);
    }
}